package com.wjy.icu.service.impl;

import com.wjy.icu.common.exception.BusinessException;
import com.wjy.icu.common.utils.PasswordUtil;
import com.wjy.icu.common.utils.RandomUtil;
import com.wjy.icu.domain.*;
import com.wjy.icu.domain.vo.MemberDto;
import com.wjy.icu.domain.vo.UserDto;
import com.wjy.icu.mapper.EquipmentUserLevelMapper;
import com.wjy.icu.mapper.IcuuserMapper;
import com.wjy.icu.service.IEquipmentService;
import com.wjy.icu.service.ISmsService;
import com.wjy.icu.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @Description:
 * @author: wangjunyong
 * @date: 2021/10/26 11:33
 */
@Service
public class UserServiceImpl implements IUserService {
    @Resource
    private IcuuserMapper userMapper;
    @Autowired
    private ISmsService smsService;
    @Resource
    private EquipmentUserLevelMapper equipmentUserLevelMapper;
    @Autowired
    IEquipmentService equipmentService;

    @Override
    public Icuuser register(UserDto user) {
        smsService.verify(user.getTel(),user.getSmsType(),user.getCode());
        Icuuser old = selectPetUserByPhone(user.getTel());
        if (old != null) {
            throw new BusinessException("手机号已存在，不允许注册");
        }
        String password = user.getPassward();
        user.setStatus(1);
        user.setGmtCreate(LocalDateTime.now());
        user.setUserCert(password);
        String salt = RandomUtil.randomStr(6);
        user.setSalt(salt);
        user.setPassward(PasswordUtil.encryptPassword(user.getTel(), user.getPassward(),salt) );
        userMapper.insertSelective(user);
        return user;
    }

    @Override
    public Icuuser selectPetUserByPhone(String phone) {
        IcuuserExample example = new IcuuserExample();
        example.createCriteria().andTelEqualTo(phone);
        List<Icuuser> users = userMapper.selectByExample(example);
        return users.size()>0?users.get(0):null;
    }

    @Override
    public Icuuser selectPetUserById(Long userId) {
        return userMapper.selectByPrimaryKey(userId.intValue());
    }

    @Override
    public Icuuser login(UserDto user) {

        return null;
    }

    @Override
    public List<EquipmentUserLevel> grouplListEquip(Integer userId) {
        EquipmentUserLevelExample example = new EquipmentUserLevelExample();
        example.createCriteria().andUserIdEqualTo(userId);
        List<EquipmentUserLevel> equipmentUserLevels = equipmentUserLevelMapper.selectByExample(example);
        equipmentUserLevels.stream().forEach(equipmentUserLevel -> {
            Equipment equipment = equipmentService.getEquipById(equipmentUserLevel.getEquipmentId());
            equipmentUserLevel.setEquipment(equipment);
        });
        return equipmentUserLevels;
    }

    @Override
    public List<EquipmentUserLevel> grouplListUser(Integer userId) {
        EquipmentUserLevelExample example = new EquipmentUserLevelExample();
        example.createCriteria().andUserIdEqualTo(userId).andLevelEqualTo(1);
        EquipmentUserLevel equipmentUserLevel = equipmentUserLevelMapper.selectOneByExample(example);
        if (equipmentUserLevel!=null){
            Integer equipmentId = equipmentUserLevel.getEquipmentId();
            EquipmentUserLevelExample example1 =  new EquipmentUserLevelExample();
            example1.createCriteria().andEquipmentIdEqualTo(equipmentId);
            List<EquipmentUserLevel> equipmentUserLevels = equipmentUserLevelMapper.selectByExample(example1);
            equipmentUserLevels.stream().forEach(x -> {
                Icuuser icuuser = userMapper.selectByPrimaryKey(x.getUserId());
                x.setUser(icuuser);
            });
            return equipmentUserLevels;
        }else {
            throw new BusinessException("暂未组建");
        }
    }

    @Override
    public void addGroupMember(MemberDto memberDto) {
        IcuuserExample userExample = new IcuuserExample();
        userExample.createCriteria().andTelEqualTo(memberDto.getPhone());
        Icuuser icuuser = userMapper.selectOneByExample(userExample);
        if (icuuser==null){
            throw new BusinessException("当前用户不存在！");
        }
        EquipmentUserLevelExample equipmentUserLevelExample = new EquipmentUserLevelExample();
        equipmentUserLevelExample.createCriteria().andUserIdEqualTo(memberDto.getUserId()).andLevelEqualTo(1);
        EquipmentUserLevel equipmentUserLevel = equipmentUserLevelMapper.selectOneByExample(equipmentUserLevelExample);
        if (equipmentUserLevel==null){
            throw new BusinessException("没有权限进行操作！");
        }
        EquipmentUserLevelExample example = new EquipmentUserLevelExample();
        example.createCriteria().andUserIdEqualTo(icuuser.getId()).andEquipmentIdEqualTo(equipmentUserLevel.getEquipmentId());
        List<EquipmentUserLevel> equipmentUserLevels = equipmentUserLevelMapper.selectByExample(example);
        if (equipmentUserLevels.size()>0){
            //已存在
        }else {
            EquipmentUserLevel equipmentUserLevel1 = new EquipmentUserLevel();
            equipmentUserLevel1.setEquipmentId(equipmentUserLevel.getEquipmentId());
            equipmentUserLevel1.setUserId(icuuser.getId());
            equipmentUserLevel1.setLevel(2);
            equipmentUserLevel1.setMac(equipmentUserLevel.getMac());
            equipmentUserLevel1.setGmtDate(LocalDateTime.now());
            equipmentUserLevelMapper.insertSelective(equipmentUserLevel1);
        }
    }

    @Override
    public void deleteGroupMember(MemberDto memberDto) {
        IcuuserExample userExample = new IcuuserExample();
        userExample.createCriteria().andTelEqualTo(memberDto.getPhone());
        Icuuser icuuser = userMapper.selectOneByExample(userExample);
        if (icuuser==null){
            throw new BusinessException("当前用户不存在！");
        }
        if (icuuser.getId().equals(memberDto.getUserId())){
            //自己人 不要删
            throw new BusinessException("不要把自己删了");
        }
        EquipmentUserLevelExample equipmentUserLevelExample = new EquipmentUserLevelExample();
        equipmentUserLevelExample.createCriteria().andUserIdEqualTo(memberDto.getUserId()).andLevelEqualTo(1);
        EquipmentUserLevel equipmentUserLevel = equipmentUserLevelMapper.selectOneByExample(equipmentUserLevelExample);
        if (equipmentUserLevel==null){
            throw new BusinessException("没有权限进行操作！");
        }
        EquipmentUserLevelExample example = new EquipmentUserLevelExample();
        example.createCriteria().andUserIdEqualTo(icuuser.getId()).andEquipmentIdEqualTo(equipmentUserLevel.getEquipmentId());
        List<EquipmentUserLevel> equipmentUserLevels = equipmentUserLevelMapper.selectByExample(example);
        if (equipmentUserLevels.size()>0){
            EquipmentUserLevel equipmentUserLevel1 = equipmentUserLevels.get(0);
            equipmentUserLevelMapper.deleteByPrimaryKey(equipmentUserLevel1.getId());
        }
    }
}
